package com.cf.service.impl;

import com.cf.dao.FocusDao;
import com.cf.entity.IdCardConfirm;
import com.cf.entity.Item;
import com.cf.service.FocusServeice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class FocusServeiceImpl implements FocusServeice {
    @Autowired
    private FocusDao focusDao;

    //查询总关注条数
    public int selectCountByitemid(long iid){
        return focusDao.selectCountByitemid(iid);
    }

    //添加关注
    public int insertFocus(long userid, long iid){
        return focusDao.insertFocus(userid,iid);
    }

    //取消关注
    public int deleteFocus(long userid, long iid){
        return focusDao.deleteFocus(userid,iid);
    }

    @Override
    public PageInfo<Item> selectItemByUserIds(int pagenum, int pagesize, long userid) {
        PageHelper.startPage(pagenum,pagesize);
        List<Item> items = focusDao.selectItemByUserIds(userid);
        for (Item item : items) {
            double  percentage =  item.getNmoney()/item.getAmoney()*100;
            long round = Math.round(percentage);
            item.setPercentage(round);

            //查询项目总关注条数
            int gzcount = selectCountByitemid(item.getIid());
            item.setGzcount(gzcount);

            //计算天数
            try {
                if (item.getIstart()==null||item.getIend()==null){
                    item.setDaybetween(0);
                }else {
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    Date stime =sdf.parse(sdf.format(new Date()));
                    Date etime =sdf.parse(sdf.format(item.getIend()));
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(stime);
                    long time1 = cal.getTimeInMillis();
                    cal.setTime(etime);
                    long time2 = cal.getTimeInMillis();
                    long between_days=(time2-time1)/(1000*3600*24);
                    item.setDaybetween(between_days);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        PageInfo<Item> pageInfo =new PageInfo<>(items);
        return pageInfo;
    }

    @Override
    public List<Item> selectItemByIdAndwu(long userid) {
        List<Item> items = focusDao.selectItemByUserIds(userid);

        for (Item item : items) {
            //查询项目总关注条数
            int gzcount = selectCountByitemid(item.getIid());
            item.setGzcount(gzcount);
        }
        if (items.size() > 5) {//判断list长度
            List newList = items.subList(0, 4);//取前四条数据

            return newList;//返回新的list
        } else {
            return items;
        }
    }
    @Override
    public int updategz(long userid, long iid) {
        int i = focusDao.selectByuidAndiid(userid, iid);
        if (i==1){
            deleteFocus(userid, iid);
            return 1;
        }else{
            insertFocus(userid, iid);
            return 2;
        }
    }
    @Override
    public int selectByuidAndiid(long userid, long iid) {
        return focusDao.selectByuidAndiid(userid,iid);
    }
}
